package com.hsgene.consumables.controller;

import com.hsgene.common.RangeType;
import com.hsgene.common.UsedType;
import com.hsgene.common.util.result.ApiResult;
import com.hsgene.common.util.support.LimitHelper;
import com.hsgene.common.util.support.OrderHelper;
import com.hsgene.common.util.support.Pagination;
import com.hsgene.consumables.condition.UnitCondition;
import com.hsgene.consumables.domain.ConsumablesUnitInfo;
import com.hsgene.consumables.exception.ConsumablesErrorCode;
import com.hsgene.consumables.service.ConsumablesUnitService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;


/**
 * 库存单位
 *
 * @author wxf
 * @date 2018/8/15 17:26
 **/
@RestController
@RequestMapping("/v1.0/genetic/testing/goods/unity")
public class ConsumablesUnitController {

    /**
     * LOG
     */
    private static final Logger LOGGER = LogManager.getLogger(ConsumablesUnitController.class);

    @Autowired
    private ConsumablesUnitService unitService;

    /**
     * 添加耗材单位
     *
     * @param unitInfo 耗材单位信息
     * @return
     */
    @PostMapping
    public ApiResult<Integer> addUnitInfo(@RequestBody ConsumablesUnitInfo unitInfo) {
            Date date = new Date();
            unitInfo.setCreateDateTime(date);
            unitInfo.setUpdateDateTime(date);
            unitInfo.setDeleteFlag(0);
            unitInfo.setAmount(0);
            int result = unitService.addUnitInfo(unitInfo);
            if(result==-1){
                return new  ApiResult(300020, "已经存在完全相同的数据，请修改信息", "已经存在完全相同的数据，请修改信息", result);
            }
            return new ApiResult(200, "success", "添加成功", result);
    }


    /**
     * 查询耗材单位信息
     *
     * @param
     * @return
     */
    @GetMapping
    public ApiResult queryUnitInfo(Integer page, Integer size, String stockUnit
            ,@RequestParam(name = "range_type",required = false)String rangeType) {
            if(rangeType!=null&& RangeType.ALL.getMessage().equals(rangeType)){
                return unitService.queryUnitInfoAll();
            }else{
                UnitCondition unitCondition = new UnitCondition();
                unitCondition.setLimitHelper(LimitHelper.create(page, size, true));
                unitCondition.setOrderHelper(OrderHelper.create("update_date_time", false));
                unitCondition.setStockUnit(stockUnit);
                Pagination<ConsumablesUnitInfo> result = unitService.queryUnitInfo(unitCondition);
                return ApiResult.succ(result);
            }
    }

    /**
     *
     * @return
     */
    @GetMapping("all")
    public ApiResult<List<ConsumablesUnitInfo>> queryUnitInfoAll() {
       return unitService.queryUnitInfoAll();
    }

    /**
     *
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public ApiResult<ConsumablesUnitInfo> getUnitDetailsInfo(@PathVariable("id") String id) {
        try {
            ConsumablesUnitInfo result = unitService.getUnitDetailsInfo(id);
            return new ApiResult(200, "success", "查询成功", result);
        } catch (Exception e) {
            LOGGER.error("add unit details info is error...", e);
            return ApiResult.fail(ConsumablesErrorCode.QUERY_IS_ERROR);
        }
    }

    /**
     * 更新耗材单位信息
     *
     * @param unitInfo
     * @return
     */
    @PutMapping("{id}")
    public ApiResult<Boolean> updateUnitInfo(@PathVariable("id")String id,@RequestBody ConsumablesUnitInfo unitInfo) {
        try {
            unitInfo.setId(id);
            ConsumablesUnitInfo unitDetailsInfo = unitService.getUnitDetailsInfoByInfo(unitInfo);
            if(unitDetailsInfo!=null&&!unitDetailsInfo.getId().equals(id)){
                return new  ApiResult(300020, "已经存在完全相同的数据，请修改信息", "已经存在完全相同的数据，请修改信息", null);
            }
            boolean result = unitService.updateUnitInfo(unitInfo);
            //false说明数据已存在
            if (result) {
                return new ApiResult(200, "success", "更新成功", result);
            }
            return ApiResult.fail(ConsumablesErrorCode.UPDATE_IS_EXISTS, result);
        } catch (Exception e) {
            LOGGER.error("add unit info is error...", e);
            return ApiResult.fail(ConsumablesErrorCode.UPDATE_IS_ERROR);
        }
    }

    /**
     * 删除耗材单位信息
     *
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public ApiResult<Boolean> deleteUnitInfo(@PathVariable("id") String id) {
        return unitService.deleteUnitInfo(id);
    }

    /**
     * 批量删除
     *
     * @param infoList
     * @return
     */
    @DeleteMapping
    public ApiResult<ConsumablesUnitInfo> deleteUnitInfoMore(@RequestBody List<ConsumablesUnitInfo> infoList) {
        return unitService.deleteUnitInfoMore(infoList);
    }
}
